package com.androidvizlab.bachelor.datamodels;

import android.util.Log;

/**
 * This class is a model of the 3D Motion tracking programs options file.
 * Any changes in value within the variables in this class will
 * be reported/notified to an Observer(Controller class), which
 * updates the view(UI). This class is a part of an MVC - pattern.
 *  
 * @author The Hive
 *
 */
public class OptionsFileModel extends OptionsFile{
	
	private boolean checkHelpFile;
	
	public OptionsFileModel()
	{
		
	}
	
	/**
	 * Increase triplet camera group
	 */
	public void addTripletCamGrp()
	{
		setNumTripletCamGrp(getNumTripletCamGrp()+1);
	}
	
	/**
	 * Decrease triplet camera group
	 */
	public void subtractTripletCamGrp()
	{
		setNumTripletCamGrp(getNumTripletCamGrp()-1);
	}
	
	/**
	 * Overridden method: prevents the value to be less than 0
	 */
	@Override
	public void setNumTripletCamGrp(int numTripletCamGrp) {
		
		if(numTripletCamGrp < 1)
		{
			super.setNumTripletCamGrp(1);
		}
		else
		{
			super.setNumTripletCamGrp(numTripletCamGrp);
		}
	}
	
	/**
	 * Overridden method: prevents the value to be less than 0
	 */
	@Override
	public void setNumMarkers(int numMarkers) {
		
		if(numMarkers < 3)
		{
			super.setNumMarkers(3);
		}
		else
		{
			super.setNumMarkers(numMarkers);
		}
	}
	
	//*** PROGRAM OUTPUT SOCKET ***//

	public void setProgramOutputSocketConnection(
			String programOutputSocketConnection) {
		
		super.setProgramOutputSocketConnection(getBooleanValue(programOutputSocketConnection));
	}
	
	//*** IMAGE FILE IN/OUTPUT ***//
	
	//Image file Input
	
	@Override
	public void setImgFileInputTriplets(boolean value)
	{
		super.setImgFileInputTriplets(value);
		//super.setImgFileInputTripletsTurnedRight(!value);
	}
	
	public void setImgFileInputTriplets(String imgFileInputTriplets) {
		setImgFileInputTriplets(getBooleanValue(imgFileInputTriplets));
	}

	@Override
	public void setImgFileInputTripletsTurnedRight(boolean value)
	{
		super.setImgFileInputTripletsTurnedRight(value);
		//super.setImgFileInputTriplets(!value);
	}
	
	public void setImgFileInputTripletsTurnedRight(
			String imgFileInputTripletsTurnedRight) {
		setImgFileInputTripletsTurnedRight(getBooleanValue(imgFileInputTripletsTurnedRight));
	}
	
	//Image file output
	
	@Override
	public void setImgFileOutputOriginalImg(boolean value)
	{
		super.setImgFileOutputOriginalImg(value);
		super.setImgFileOutputGeneratedTripletsTurnedRight(!value);
		super.setImgFileOutputGeneratedTriplets(!value);		
	}
	
	public void setImgFileOutputOriginalImg(String imgFileOutputOriginalImg) {
		setImgFileOutputOriginalImg(getBooleanValue(imgFileOutputOriginalImg));
	}
	
	@Override
	public void setImgFileOutputGeneratedTriplets(boolean value)
	{
		super.setImgFileOutputGeneratedTriplets(value);
		super.setImgFileOutputGeneratedTripletsTurnedRight(!value);
		super.setImgFileOutputOriginalImg(!value);		
	}

	public void setImgFileOutputGeneratedTriplets(
			String imgFileOutputGeneratedTriplets) {
		setImgFileOutputGeneratedTriplets(getBooleanValue(imgFileOutputGeneratedTriplets));
	}

	public void setImgFileOutputGeneratedTripletsTurnedRight(boolean value)
	{
		super.setImgFileOutputGeneratedTripletsTurnedRight(value);
		super.setImgFileOutputGeneratedTriplets(!value);
		super.setImgFileOutputOriginalImg(!value);
	}
	
	public void setImgFileOutputGeneratedTripletsTurnedRight(
			String imgFileOutputGeneratedTripletsTurnedRight) {
		setImgFileOutputGeneratedTripletsTurnedRight(getBooleanValue(imgFileOutputGeneratedTripletsTurnedRight));
	}
	
	//*** HELP FILE ***//
	
	//Help file sequence data
	
	public boolean getCheckHelpFileValue()
	{
		return checkHelpFile;
	}
	
	public void setCheckHelpFileValue(boolean value)
	{
		checkHelpFile = value;
		super.setHelpFileOutputImageDetectPoints(value);
		super.setHelpFileOutputDuplicatePoints(value);
		super.setHelpFileOutputMatch3(value);
		super.setHelpFileOutputdobbelMatch(value);
		super.setHelpFileOutputConnectPoints(value);
		super.setHelpFileOutputTimeseries(value);
		
		notifyObservers();
	}

	public void setHelpFileOutputImageDetectPoints(
			String helpFileOutputImageDetectPoints) {

		super.setHelpFileOutputImageDetectPoints(getBooleanValue(helpFileOutputImageDetectPoints));
	}

	public void setHelpFileOutputDuplicatePoints(
			String helpFileOutputDuplicatePoints) {
		
		super.setHelpFileOutputDuplicatePoints(getBooleanValue(helpFileOutputDuplicatePoints));
	}

	//Help file Match
	
	public void setHelpFileOutputMatch3(String helpFileOutputMatch3) {

		super.setHelpFileOutputMatch3(getBooleanValue(helpFileOutputMatch3));
	}

	public void setHelpFileOutputdobbelMatch(String helpFileOutputdobbelMatch) {

		super.setHelpFileOutputdobbelMatch(getBooleanValue(helpFileOutputdobbelMatch));
	}

	public void setHelpFileOutputConnectPoints(
		String helpFileOutputConnectPoints) {
		
		super.setHelpFileOutputConnectPoints(getBooleanValue(helpFileOutputConnectPoints));
	}

	public void setHelpFileOutputTimeseries(String helpFileOutputTimeseries) {

		super.setHelpFileOutputTimeseries(getBooleanValue(helpFileOutputTimeseries));
	}
	
	//HELPER METHOD 
	
	/**
	 * Return a boolean value from string input.
	 * Return true if input equals T and false otherwise
	 * 
	 * @param input string value to be converted to boolean
	 * @return a boolean value 
	 */
	public boolean getBooleanValue(String input)
	{
		if(input.equalsIgnoreCase("T"))
		{
			return true;
		}
		else
		{
			return false;
		}
	}

	/**
	 * Convert boolean input to string value T or F
	 * @param input boolean input
	 * @return T for true and F for false
	 */
	public String getStringValueBooleanInput(boolean input)
	{
		if(input)
		{
			return "T";
		}
		else
		{
			return "F";
		}
	}
	
	/**
	 * Return the RunType string value
	 * @return string value
	 */
	public String getRunTypeStringValue() {

		return getRunType().getRunType();
	}

	/**
	 * Convert the string input to appropriate RunType
	 * and set the Models RunType
	 * @param runType string RunType value
	 */
	public void setRunType(String runType) {

		setRunType(getRunTypeValue(runType));
	}

	/**
	 * Helper method: converts string input to OptionsFile.RunType
	 * @param input string input
	 * @return string input as RunType
	 */
	public RunType getRunTypeValue(String input)
	{
		if(input.equals(RunType.CALIBRATION.getRunType()))
		{
			return RunType.CALIBRATION;
		}
		else if(input.equals(RunType.PRODUCTION.getRunType()))
		{
			return RunType.PRODUCTION;
		}
		else
		{
			return RunType.RECALCULATION;
		}
	}
	
	//Validate data
	/**
	 * Return a VizlabInputData containing the values specified by the user
	 * @return a VizlabInputData containing the values specified by the user
	 */
	public VizlabInputData sendData()
	{
		VizlabInputData input = new VizlabInputData();
		
		input.setCalibrationFilePath(getCalibrationFilePath());
		input.setCalibrationFileName(getCalibrationFileName());
		input.setRunType(getRunTypeStringValue());
		input.setProgramOutputSocketConnection(getStringValueBooleanInput(isProgramOutputSocketConnection()));
		input.setNumTripletCamGrp(getNumTripletCamGrp());
		input.setNumMarkers(getNumMarkers());
		input.setNumTimePts(getNumTimePts());
		input.setTriggingInterval(getTriggingInterval());
		input.setApproxFrameMarkerLimit(getApproxFrameMarkerLimit());
		
		input.setImgFileInputTriplets(getStringValueBooleanInput(isImgFileInputTriplets()));
		input.setImgFileInputTripletsTurnedRight(getStringValueBooleanInput(isImgFileInputTripletsTurnedRight()));
		input.setImgFileOutputOriginalImg(getStringValueBooleanInput(isImgFileOutputOriginalImg()));
		input.setImgFileOutputGeneratedTriplets(getStringValueBooleanInput(isImgFileOutputGeneratedTriplets()));
		input.setImgFileOutputGeneratedTriplets(getStringValueBooleanInput(isImgFileOutputGeneratedTripletsTurnedRight()));
		
		input.setHelpFileOutputImageDetectPoints(getStringValueBooleanInput(isHelpFileOutputImageDetectPoints()));
		input.setHelpFileOutputDuplicatePoints(getStringValueBooleanInput(isHelpFileOutputDuplicatePoints()));
		input.setHelpFileOutputMatch3(getStringValueBooleanInput(isHelpFileOutputMatch3()));
		input.setHelpFileOutputdobbelMatch(getStringValueBooleanInput(isHelpFileOutputdobbelMatch()));
		input.setHelpFileOutputConnectPoints(getStringValueBooleanInput(isHelpFileOutputConnectPoints()));
		input.setHelpFileOutputTimeseries(getStringValueBooleanInput(isHelpFileOutputTimeseries()));
		
		input.setOffsetPix(getOffsetPix());
		input.setMinpix(getMinpix());
		input.setMaxpix(getMaxpix());
		input.setMaxerr(getMaxerr());
		input.setMinsep(getMinsep());
		
		return input;
	}
}